﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic;
using System.Diagnostics;

namespace Maticsoft.Common
{
    public sealed class netping
    {
        public static bool pingIP(string ip)
        {
            Microsoft.VisualBasic.Devices.Computer com = new Microsoft.VisualBasic.Devices.Computer();
            return com.Network.Ping(ip);
        }

        public static string pingIPcmd(string ip,int packagebag)
        {
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.FileName = "ping ";
            if (packagebag > 0 && packagebag < 65500)
            {
                p.StartInfo.Arguments = ip + string.Format(" -l {0}", packagebag);
            }
            else
            {
                p.StartInfo.Arguments = ip;
            }
            p.Start();
            p.WaitForExit();
            string output = p.StandardOutput.ReadToEnd();
            return output;
        }
        public static string getMAC(string ip)
        {
            try
            {
                if (pingIP(ip))
                {
                    Process p = new Process();
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.StartInfo.RedirectStandardError = true;
                    p.StartInfo.CreateNoWindow = true;
                    p.StartInfo.FileName = "nbtstat";
                    p.StartInfo.Arguments = "-A " + ip;
                    p.Start();
                    p.WaitForExit();
                    string output = p.StandardOutput.ReadToEnd();
                    return output.Substring(output.LastIndexOf("=") + 1).Trim();
                }
                else
                {
                    return "主机不存在或不在线!";
                }
            }
            catch
            {
                throw;
            }
        }
    }
}
